import { Component } from 'react'
import { View } from '@tarojs/components'
import './index.scss'
import Taro from '@tarojs/taro'
import dayjs from "dayjs"

//组件
import { LgPopLayer } from '../../../../components/popLayer'
import LgButton from '../../../../components/button'

interface IndexState{
  chooseDate?:string
  allPersonCount:number
}

export default class PrPopChooseObject extends Component <any,IndexState>{
  constructor(props) {
    super(props)
    this.state={
      allPersonCount:0,
    }

    this.changePerson = this.changePerson.bind(this)
    this.deleteItem = this.deleteItem.bind(this)
  }

  componentWillMount() { }

  componentDidMount() { }

  componentWillUnmount() { }

  changePerson(){
    this.setState({
      allPersonCount:360
    })
  }

  getAllpersonCount(data){
    let allpersonCount = 0
    // console.log("选择信息",data)
    if(data?.identities_pop?.length){
      data.identities_pop.map(item=>{
        if(item.Info.count){
          allpersonCount = allpersonCount + item.Info.count*1
        }
      })
    }
    if(data?.teachers_pop?.length){
      data.teachers_pop.map(item=>{
        if(item.Info.count){
          allpersonCount = allpersonCount + item.Info.count*1
        }
      })
    }
    if(data?.students_pop?.length){
      data.students_pop.map(item=>{
        if(item.Info.studentCount){
          allpersonCount = allpersonCount + item.Info.studentCount*1
        }else if(item.Info.count){
          allpersonCount = allpersonCount + item.Info.count*1
        }
      })
    }
    if(data?.parents_pop?.length){
      data.parents_pop.map(item=>{
        if(item.Info.studentCount){
          allpersonCount = allpersonCount + item.Info.studentCount*1
        }else if(item.Info.count){
          allpersonCount = allpersonCount + item.Info.count*1
        }
      })
    }
    if(data?.departments_pop?.length){
      data.departments_pop.map(item=>{
        if(item.Info.totalCount){
          allpersonCount = allpersonCount + item.Info.totalCount*1
        }
      })
    }
    if(data?.persons_pop?.length){
      allpersonCount = allpersonCount + data.persons_pop.length*1
    }
    if(data?.grades_pop?.length){
      data.grades_pop.map(item=>{
        if(item.Info.totalCount){
          allpersonCount = allpersonCount + item.Info.totalCount*1
        }
      })
    }
    if(data?.classes_pop?.length){
      data.classes_pop.map(item=>{
        if(item.Info.totalCount){
          allpersonCount = allpersonCount + item.Info.totalCount*1
        }
      })
    }

    return allpersonCount
  }

  deleteItem(item){
    this.props.deleteItem?this.props.deleteItem(item):""
  }
  deleteItem2(item,type,index){//移除2
    this.props.deleteItem2?this.props.deleteItem2(item, type, index):""
  }

  isEmpty(data, receivers){
    let allCount = 0
    if(data?.identities_pop?.length){
      allCount = allCount + data.identities_pop.length
    }
    if(data?.teachers_pop?.length){
      allCount = allCount + data.teachers_pop.length
    }
    if(data?.students_pop?.length){
      allCount = allCount + data.students_pop.length
    }
    if(data?.parents_pop?.length){
      allCount = allCount + data.parents_pop.length
    }
    if(data?.departments_pop?.length){
      allCount = allCount + data.departments_pop.length
    }
    if(data?.persons_pop?.length){
      allCount = allCount + data.persons_pop.length
    }
    if(data?.grades_pop?.length){
      allCount = allCount + data.grades_pop.length
    }
    if(data?.classes_pop?.length){
      allCount = allCount + data.classes_pop.length 
    }
    //==============================================
    if(receivers?.identities?.length){
      allCount = allCount + receivers.identities.length
    }
    if(receivers?.teachers?.length){
      allCount = allCount + receivers.teachers.length
    }
    if(receivers?.students?.length){
      allCount = allCount + receivers.students.length
    }
    if(receivers?.parents?.length){
      allCount = allCount + receivers.parents.length
    }
    if(receivers?.departments?.length){
      allCount = allCount + receivers.departments.length
    }
    if(receivers?.persons?.length){
      allCount = allCount + receivers.persons.length
    }
    if(receivers?.grades?.length){
      allCount = allCount + receivers.grades.length
    }
    if(receivers?.classes?.length){
      allCount = allCount + receivers.classes.length 
    }

    if(allCount == 0){
      return true
    }else{
      return false
    }
  }
  //渲染处理=============================================================
  getIdentities(data,receivers){
    
  }

  //=====================================================================
  render() {
    let data = this.props.data
    let receivers = this.props.receivers
    let isEmpty = this.isEmpty(data, receivers)
    let allpersonCount = this.getAllpersonCount(data) || receivers.persons?.length || 0
    console.log("弹窗信息",data,receivers)

    // let identities = this.getIdentities(data.identities_pop, receivers.identities)

    return (
      <View className='PrPopChooseObject'>
        <LgPopLayer
            isOpen={this.props.isOpen}
            showLayer={this.props.onClosePop}
            position="bottom"
            title={"已选择:"+allpersonCount+"人"}
            showClose
            customContentClass="lg_choObj_select_pop"
        >
          <View className='content'>
            {//管理员==================================================================================================
              data?.identities_pop?.map((item,index)=>{
                let personCount = item.Info?.count
                return(
                  <View className='item' key={"admin"+index}>
                    <View className='text'>[管理员]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.identities?.map((item,index)=>{
                return(
                  <View className='item' key={"admin"+index}>
                    <View className='text'>[管理员]{item.identityName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"identities",index)}>移除</View>
                  </View>
                )
              })
            }

            {//教师==================================================================================================
              data?.teachers_pop?.map((item,index)=>{
                let personCount = item.Info?.teacherCount || item.Info?.count
                return(
                  <View className='item' key={"teachers"+index}>
                    <View className='text'>[教师]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.teachers?.map((item,index)=>{
                return(
                  <View className='item' key={"teachers"+index}>
                    <View className='text'>[教师]{item.subjectName || item.groupName || item.collegeName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"teachers",index)}>移除</View>
                  </View>
                )
              })
            }

            {//学生==================================================================================================
              data?.students_pop?.map((item,index)=>{
                let personCount = item.Info?.studentCount || item.Info?.count
                return(
                  <View className='item' key={"students"+index}>
                    <View className='text'>[学生]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.students?.map((item,index)=>{
                return(
                  <View className='item' key={"students"+index}>
                    <View className='text'>[学生]{item.className || item.gradeName || item.majorName || item.collegeName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"students",index)}>移除</View>
                  </View>
                )
              })
            }

            {//家长==================================================================================================
              data?.parents_pop?.map((item,index)=>{
                let personCount = item.Info?.studentCount || item.Info?.count//家长数量和学生数量一样
                return(
                  <View className='item' key={"parents"+index}>
                    <View className='text'>[家长]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.parents?.map((item,index)=>{
                return(
                  <View className='item' key={"parents"+index}>
                    <View className='text'>[家长]{item.className || item.gradeName || item.majorName || item.collegeName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"parents",index)}>移除</View>
                  </View>
                )
              })
            }


            {//部门==================================================================================================
              data?.departments_pop?.map((item,index)=>{
                let personCount = item.Info?.totalCount
                return(
                  <View className='item' key={"departments"+index}>
                    <View className='text'>[部门]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.departments?.map((item,index)=>{
                return(
                  <View className='item' key={"departments"+index}>
                    <View className='text'>[部门]{item.departmentName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"departments",index)}>移除</View>
                  </View>
                )
              })
            }

            {//个人==================================================================================================
              data?.persons_pop?.map((item,index)=>{
                let personCount = item.Info?.personCount
                return(
                  <View className='item' key={"admin"+index}>
                    <View className='text'>[个人]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.persons?.map((item,index)=>{
                return(
                  <View className='item' key={"admin"+index}>
                    <View className='text'>[个人]{item.userName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"persons",index)}>移除</View>
                  </View>
                )
              })
            }
            {//年级==================================================================================================
              data?.grades_pop?.map((item,index)=>{
                let personCount = item.Info?.totalCount
                return(
                  <View className='item' key={"grades"+index}>
                    <View className='text'>[年级]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.grades?.map((item,index)=>{
                return(
                  <View className='item' key={"grades"+index}>
                    <View className='text'>[年级]{item.gradeName}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"grades",index)}>移除</View>
                  </View>
                )
              })
            }
            {//班级==================================================================================================
              data?.classes_pop?.map((item,index)=>{
                let personCount = item.Info?.totalCount || item.Info?.count
                return(
                  <View className='item' key={"classes"+index}>
                    <View className='text'>[班级]{item.name}{personCount?`(${personCount}人)`:""}</View>
                    <View className='button' onClick={()=>this.deleteItem(item)}>移除</View>
                  </View>
                )
              })
            }
            {
              receivers?.classes?.map((item,index)=>{
                return(
                  <View className='item' key={"classes"+index}>
                    <View className='text'>[班级]{item.className}</View>
                    <View className='button' onClick={()=>this.deleteItem2(item,"classes",index)}>移除</View>
                  </View>
                )
              })
            }

            {
              isEmpty?
              <View className='empty_box'>
                <View className='empty'>未选择对象</View>
              </View>
              :""
            }
            <View className='bottom'></View>
          </View>
          
        </LgPopLayer>
      </View>
    )
  }
}
